package com.ycsj.goldmedalnewconcept.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.adapter.ClassInfoAdapter;
import com.ycsj.goldmedalnewconcept.bean.ClassUseInfo;
import com.ycsj.goldmedalnewconcept.bean.PersonInfo;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.GsonUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.UiHelper;
import com.ycsj.goldmedalnewconcept.view.MyListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期：2017/11/9 8:46 描述:班级信息界面 作者:张伟
 */

@SuppressLint("NewApi")
public class ClassInfoActivity extends Activity implements View.OnClickListener {

	// 是否是第一次加载
	private boolean isFirst = true;

	// 正常加载
	public static final int NORMAL = 0X110;

	// 显示班级的线性布局
	LinearLayout llClass;

	// 返回
	private LinearLayout llBack;

	// 标题右边的编辑
	private TextView tvEdit;

	// 班级名称的TextView
	private TextView tvname;

	// 加载
	private LinearLayout llLoading;

	// 添加班级的图片
	private ImageView lvAddClass;

	// 空数据
	private LinearLayout llEmpty;

	// 列表控件
	private MyListView lv;

	// 列表数据集合
	private List<ClassUseInfo> oldDatas;
	private ClassInfoAdapter adapter;

	// 用于保存小组头像的集合
	private List<Integer> teamIcons = new ArrayList<Integer>();
	// 用于保存小组名称的集合
	private List<String> teamNames = new ArrayList<String>();

	// 用于记录上个页面传递过来的值
	private String classNum;// 班级的num

	private String classId;// 班级的id

	private String icon;// 班級的头像

	private String className;// 班级名称

	// 用于保存已经使用的的班级名称的集合
	private List<String> hasUsedNames;

	// 用于保存已经使用的的班级头像的集合
	private List<String> hasUsedNums;

	// 跳转到创建班级的请求码
	public static final int ADDCLASSTEAM = 0x170;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_class_info);

		// 设置状态栏
		setBar();

		// 初始化View
		initView();

		// 获取上个页面传递过来的值
		getData();

		// 设置监听
		initListener();

		// 注册广播接受者
		registerBroadCast();

		// 初始化ListView
		initLv();

		// 网络请求的方法
		http(NORMAL);
	}

	private IntentFilter intentFilter;
	private MyBroadcastReceiver myBroadcastReceiver;

	/**
	 * 动态注册广播接受者的方法
	 */
	private void registerBroadCast() {
		intentFilter = new IntentFilter();
		intentFilter.addAction("refreshbroadcast");
		myBroadcastReceiver = new MyBroadcastReceiver();
		registerReceiver(myBroadcastReceiver, intentFilter);
	}

	/**
	 * 初始化控件
	 */
	private void initView() {
		llBack = (LinearLayout) findViewById(R.id.ll_back);
		llLoading = (LinearLayout) findViewById(R.id.ll_loading);
		llEmpty = (LinearLayout) findViewById(R.id.ll_empty);
		lv = (MyListView) findViewById(R.id.lv);
		tvEdit = (TextView) findViewById(R.id.tv_edit);
		tvname = (TextView) findViewById(R.id.tv_name);
		lvAddClass = (ImageView) findViewById(R.id.lv_add);
		llClass = (LinearLayout) findViewById(R.id.ll_class);
		// 初次进入不显示
		lvAddClass.setVisibility(View.INVISIBLE);
	}

	/**
	 * 获取上个页面传递过来的值
	 *
	 * @return
	 */
	public void getData() {

		// 获取classid
		classId = getIntent().getStringExtra("classid");

		// 获取classnum
		classNum = getIntent().getStringExtra("classnum");

		// 获取班级头像
		icon = getIntent().getStringExtra("icon");

//		Log.i("test", "icon的值是:" + icon);

		// 获取班级名称
		className = getIntent().getStringExtra("classname");

		if (className != null) {
			tvname.setText(className);
		}

		// 获取已经使用过的班级名称集合的json字符串
		String hasUsedNameJson = getIntent().getStringExtra("names");
		if (!TextUtils.isEmpty(hasUsedNameJson)) {
			hasUsedNames = GsonUtil.jsonToList(hasUsedNameJson, String[].class);
		} else {
			hasUsedNames = new ArrayList<String>();
		}

		// 获取使用过的班级头像的num集合的json字符串
		String hasUsedNumJson = getIntent().getStringExtra("nums");
		if (!TextUtils.isEmpty(hasUsedNumJson)) {
			hasUsedNums = GsonUtil.jsonToList(hasUsedNameJson, String[].class);
		} else {
			hasUsedNums = new ArrayList<String>();
		}
	}

	/**
	 * 设置监听方法
	 */
	private void initListener() {
		llBack.setOnClickListener(this);
		tvEdit.setOnClickListener(this);
		lvAddClass.setOnClickListener(this);
		llClass.setOnClickListener(this);
	}

	/**
	 * 初始化ListView的方法
	 */
	private void initLv() {
		oldDatas = new ArrayList<ClassUseInfo>();
	}

	/**
	 * 网络请求的方法
	 *
	 * @param method
	 */
	private void http(final int method) {
		OkHttpClient mOkHttpClient = new OkHttpClient();
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "4");
		builder.add("userID", SPUtil.getString(this, "account", ""));
		builder.add("classnum", classNum);
		Request request = new Request.Builder()
				.url(Constant.SERVER_183 + "ycsj_jpxgn/servlet/MessageController")
//				.url(Constant.SERVER_183 + "ycsj_count/KeJianMessage")
				
				.post(builder.build()).build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {
			@Override
			public void onFailure(Request request, IOException e) {
				isFirst = false;
				// 请求失败
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						// 请求失败,主线程更新UI操作
						if (method == NORMAL) {
							if (llLoading != null) {
								llLoading.setVisibility(View.GONE);
							}
						}
						Toast.makeText(ClassInfoActivity.this, "网络错误",
								Toast.LENGTH_SHORT).show();
					}
				});
			}

			@Override
			public void onResponse(final Response response) throws IOException {
				try {
					// 请求成功
					isFirst = false;
					// 获取服务器返回的json数据
					final String json = response.body().string();
					Log.e("test", json);
					if (!TextUtils.isEmpty(json)) {
						// 获取新的数据
						final List<ClassUseInfo> newDatas = new ArrayList<ClassUseInfo>();
						// 使用JSONObject来解析服务器返回的数据格式
						JSONObject jsonObject = new JSONObject(json);
						JSONObject teamlist = jsonObject.getJSONObject(
								"classinfo").getJSONObject("teamlist");
						for (int i = 0; i < teamlist.length(); i++) {
							// 创建ListView的数据模型
							JSONObject object = teamlist.getJSONObject("team"
									+ i);
							ClassUseInfo classUseInfo = new ClassUseInfo();

							// 设置小组的名称
							classUseInfo.setName(object.getString("name"));
							teamNames.add(object.getString("name"));

							// 设置小组的classNum
							classUseInfo.setClassnum(object.getInt("classnum")
									+ "");

							// 设置小组的未知
							classUseInfo.setExp(object.getInt("exp"));
							// 设置小组的icon
							classUseInfo.setIcon(object.getInt("icon") + "");

							// 同时将小组的icon保存到集合中
							teamIcons.add(object.getInt("icon"));

							// 设置小组的money
							classUseInfo.setMoney(object.getString("money"));

							// 设置小组的音乐
							classUseInfo.setMusic(object.getString("music"));

							// 设置小组的Num
							classUseInfo.setNum(object.getInt("num") + "");

							// 设置小组的标语
							classUseInfo.setSlogan(object.getString("slogan"));

							// 设置Taget
							classUseInfo.setTaget(object.getString("taget"));

							// 设置小组的Teamid
							classUseInfo.setTeamID(object.getInt("teamID") + "");

							// 设置小组的userID
							classUseInfo.setUserID(object.getString("userID"));

							List<PersonInfo> infos = new ArrayList<>();
							// JSONObject brave = object.getJSONObject("brave");
							// 解析智wisdom 勇brave 欢joy 爱love 四个模型
							JSONObject wisdom = object.optJSONObject("wisdom");
							JSONObject brave = object.optJSONObject("brave");
							JSONObject joy = object.optJSONObject("joy");
							JSONObject love = object.optJSONObject("love");
							infos.add(getPerson(classUseInfo, wisdom));
							infos.add(getPerson(classUseInfo, brave));
							infos.add(getPerson(classUseInfo, joy));
							infos.add(getPerson(classUseInfo, love));
							classUseInfo.setPersonInfos(infos);
							newDatas.add(classUseInfo);
						}

						if (null != newDatas && newDatas.size() != 0) {
							// 证明有列表数据,更新主界面
							runOnUiThread(new Runnable() {
								@Override
								public void run() {
									if (method == NORMAL) {
										// 正常请求
										if (llLoading != null) {
											llLoading.setVisibility(View.GONE);
										}
										oldDatas.clear();
										oldDatas.addAll(newDatas);
										// 显示头部的班级和添加按钮
										if (oldDatas.size() >= 10) {
											lvAddClass.setVisibility(View.GONE);
										} else {
											lvAddClass
													.setVisibility(View.VISIBLE);
										}
										adapter = new ClassInfoAdapter(
												ClassInfoActivity.this,
												oldDatas,
												R.layout.item_lv_class_manager,
												getScreenWidth(),
												ClassInfoActivity.this);
										lv.setAdapter(adapter);
										tvEdit.setText("编辑");
									}
								}
							});
						} else {
							runOnUiThread(new Runnable() {
								@Override
								public void run() {
									// 证明没有数据,显示空视图,隐藏加载框
									if (method == NORMAL) {
										// 正常加载
										if (llLoading != null) {
											llLoading.setVisibility(View.GONE);
										}
										if (llEmpty != null) {
											llEmpty.setVisibility(View.GONE);
										}
										lvAddClass.setVisibility(View.VISIBLE);
									}
								}
							});
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!isFirst) {
			http(NORMAL);
		}
	}

	/**
	 * 页面控件点击事件
	 *
	 * @param view
	 */
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.ll_back:
			finish();
			break;

		case R.id.lv_add:
			// 添加班级的+号按钮,判断当前的小组数量是否超过10个,超过10个则提示不让继续添加
			if (oldDatas.size() >= 10) {
				Toast.makeText(this, "最多只能添加10个小组", Toast.LENGTH_SHORT).show();
				return;
			} else {
				// 跳转到添加班级的界面
				Intent intent = new Intent(ClassInfoActivity.this,
						CreateTeamActivity.class);
				String teamList = GsonUtil.listToJson(teamIcons);
				// 小组名称集合
				String teamNameList = GsonUtil.listToJson(teamNames);
				intent.putExtra("flag", 200);
				intent.putExtra("team", teamList);// 已经使用过的小组头像
				intent.putExtra("teamNameList", teamNameList);// 已经使用过的小组名称
				// 将班级的classNum传递给下个页面
				intent.putExtra("classnum", classNum);// 班级的班级号
				startActivityForResult(intent, ADDCLASSTEAM);
			}
			break;

		case R.id.ll_class:
			// 修改班级的信息
			showPopClass();
			break;

		case R.id.lv_left:
			// 点击左箭头
			syncLeftState();
			break;
		case R.id.lv_right:
			// 点击右边箭头
			syncRightState();
			break;
		case R.id.tv_save:
			// 点击保存的操作
			// 请求服务器,将修改的班级信息发送给服务器
			// 获取编辑的班级名称和班级头像然后进行判断
			if (hasUsedNums.contains((pos + 1))) {
				// 证明班级头像已经被使用过
				UiHelper.toast(this, "班级头像已经被使用了");
				return;
			}

			String newClassName = etClass.getText().toString().trim();// 获取班级名称

			// if (TextUtils.isEmpty(newClassName)) {
			// UiHelper.toast(this, "请输入班级名称");
			// return;
			// }
			//
			// if (hasUsedNames.contains(newClassName)) {
			// // 证明班级头像已经被使用过
			// UiHelper.toast(this, "班级名称已经被使用了");
			// return;
			// }

			// 只要修改了一项就做上传服务器
			if (Integer.parseInt(icon) != (pos + 1)
					|| !className.equals(newClassName)) {
				// 请求服务器
				httpUpdateClass((pos + 1), newClassName);
			} else {
				// 否则不需要上传服务器
				UiHelper.toast(this, "未做任何修改");
				if (popupWindow != null && popupWindow.isShowing()) {
					popupWindow.dismiss();
					popupWindow = null;
					headView = null;
				}
			}

			break;

		case R.id.tv_edit:
			// 点击编辑后的操作
			if ("编辑".equals(tvEdit.getText().toString().trim())) {
				tvEdit.setText("完成");
				for (int i = 0; i < oldDatas.size(); i++) {
					List<PersonInfo> personInfos = oldDatas.get(i)
							.getPersonInfos();
					for (int j = 0; j < personInfos.size(); j++) {
						if (!TextUtils.isEmpty(personInfos.get(j).getName())) {
							personInfos.get(j).setIsdeleted(true);
						}
					}
				}
				adapter.notifyDataSetChanged();
			} else if ("完成".equals(tvEdit.getText().toString().trim())) {
				tvEdit.setText("编辑");
				for (int i = 0; i < oldDatas.size(); i++) {
					List<PersonInfo> personInfos = oldDatas.get(i)
							.getPersonInfos();
					for (int j = 0; j < personInfos.size(); j++) {
						if (!TextUtils.isEmpty(personInfos.get(j).getName())) {
							personInfos.get(j).setIsdeleted(false);
						}
					}
				}
				adapter.notifyDataSetChanged();
			}
			break;
		}
	}

	/**
	 * 请求服务器修改班级头像和名称的方法
	 */
	private void httpUpdateClass(int newicon, final String newClassName) {
		UiHelper.showDialog(this, "修改中...");
		OkHttpClient mOkHttpClient = new OkHttpClient();
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "27");
		builder.add("userID", SPUtil.getString(this, "account", ""));
		builder.add("icon", newicon + "");
		builder.add("name", newClassName);
		builder.add("num", classNum);
		Log.i("test", "上传的头像是:" + newicon + "上传的名称是:" + newClassName);
		Request request = new Request.Builder()
				.url(Constant.SERVER_183 + "ycsj_jpxgn/servlet/MessageController")
//				.url(Constant.SERVER_183 + "ycsj_count/KeJianMessage")
				.post(builder.build()).build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {
			@Override
			public void onFailure(Request request, IOException e) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						UiHelper.dismissDialog();
						// 关闭PopUpWindow
						if (popupWindow != null && popupWindow.isShowing()) {
							popupWindow.dismiss();
							popupWindow = null;
							headView = null;
						}
						UiHelper.toast(ClassInfoActivity.this, "网络错误,修改失败");
					}
				});
			}

			@Override
			public void onResponse(final Response response) throws IOException {
				try {
					String json = response.body().string();
					JSONObject jsonObject = new JSONObject(json);
					final int state = jsonObject.getInt("state");
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							if (state == 1) {
								UiHelper.dismissDialog();
								// 关闭PopUpWindow
								if (popupWindow != null
										&& popupWindow.isShowing()) {
									popupWindow.dismiss();
									popupWindow = null;
									headView = null;
								}
								UiHelper.toast(ClassInfoActivity.this, "修改成功");

								tvname.setText(newClassName);
							} else if (state == -1) {
								// 创建小组失败
								UiHelper.dismissDialog();
								// 关闭PopUpWindow
								if (popupWindow != null
										&& popupWindow.isShowing()) {
									popupWindow.dismiss();
									popupWindow = null;
									headView = null;
								}
								UiHelper.toast(ClassInfoActivity.this, "修改失败");
							}

						}
					}

					);

				} catch (Exception e) {
					Log.i("test", "解析异常");
				}

			}
		});

	}

	/**
	 * 设置沉浸状态栏的方法,需要在setContentView()之后调用
	 */
	private void setBar() {
		// 兼容5.0+，状态栏设置透明
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			Window window = getWindow();
			window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			window.getDecorView().setSystemUiVisibility(
					View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
							| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
			window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
			window.setStatusBarColor(Color.TRANSPARENT);
		} else
		// 为了沉浸式状态栏 4.4以上才有
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}
	}

	/**
	 * 将一个JSONObject复制给PersonInfo对象
	 */
	private PersonInfo getPerson(ClassUseInfo classUseInfo, JSONObject object) {
		PersonInfo info = null;
		try {
			if (null != object) {
				info = new PersonInfo();
				info.setTeamId(classUseInfo.getTeamID());
				info.setIcon(object.getString("icon"));
				info.setBirthday(object.getString("birthday"));
				info.setTeamnum(object.getInt("teamnum"));
				info.setNickname(object.getString("nickname"));
				info.setTel(object.getString("tel"));
				info.setClassnum(object.getInt("classnum"));
				info.setFavorite(object.getString("favorite"));
				info.setId(object.getInt("id"));
				info.setUserID(object.getString("userID"));
				info.setExp(object.getInt("exp"));
				info.setSchool(object.getString("school"));
				info.setAddress(object.getString("address"));
				info.setName(object.getString("name"));
				info.setMoney(object.getInt("money"));
				info.setGender(object.getString("gender"));
				info.setGrade(object.getString("grade"));
				info.setPayment(object.getString("payment"));
				info.setIsdeleted(false);
			} else {
				info = new PersonInfo();
				info.setTeamId(classUseInfo.getTeamID());
				info.setTeamnum(Integer.parseInt(classUseInfo.getNum()));
				info.setClassnum(Integer.parseInt(classUseInfo.getClassnum()));
				info.setIsdeleted(false);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return info;
	}

	/**
	 * 获取屏幕宽度
	 */
	private int getScreenWidth() {
		WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		return wm.getDefaultDisplay().getWidth();
	}

	/**
	 * 弹出编辑班级信息的PopUpWindow
	 */
	private View headView;
	private ImageView lvDelete, lvLeft, lvRight, lvTeam;
	private TextView tvSave;
	private EditText etClass;
	private PopupWindow popupWindow;
	private int[] icons = { R.drawable.class1, R.drawable.class2,
			R.drawable.class3, R.drawable.class4, R.drawable.class5,
			R.drawable.class6, R.drawable.class7, R.drawable.class8,
			R.drawable.class9, R.drawable.class10 };
	int pos = 0;

	private void showPopClass() {
		headView = View.inflate(this, R.layout.pop_item_class_head, null);
		lvDelete = (ImageView) headView.findViewById(R.id.lv_delete);
		lvLeft = (ImageView) headView.findViewById(R.id.lv_left);
		lvRight = (ImageView) headView.findViewById(R.id.lv_right);
		etClass = (EditText) headView.findViewById(R.id.et_class);
		// 默认显示班级名称
		if (!TextUtils.isEmpty(className)) {
			etClass.setText(className);
			;
		}

		// 班級头像和
		lvTeam = (ImageView) headView.findViewById(R.id.lv_team);
		tvSave = (TextView) headView.findViewById(R.id.tv_save);
		// 默认设置班级显示的头像信息
		if ("1".equals(icon)) {
			lvTeam.setImageResource(icons[0]);
			pos = 0;
			// 初始状态将左边的箭头隐藏,右边的箭头显示
			lvLeft.setVisibility(View.INVISIBLE);
			lvRight.setVisibility(View.VISIBLE);
		} else if ("2".equals(icon)) {
			lvTeam.setImageResource(icons[1]);
			pos = 1;
			// 初始状态将左边的箭头隐藏,右边的箭头显示
			lvLeft.setVisibility(View.VISIBLE);
			lvRight.setVisibility(View.VISIBLE);
		} else if ("3".equals(icon)) {
			lvTeam.setImageResource(icons[2]);
			pos = 2;
			// 初始状态将左边的箭头隐藏,右边的箭头显示
			lvLeft.setVisibility(View.VISIBLE);
			lvRight.setVisibility(View.VISIBLE);
		} else if ("4".equals(icon)) {
			lvTeam.setImageResource(icons[3]);
			pos = 3;
			// 初始状态将左边的箭头隐藏,右边的箭头显示
			lvLeft.setVisibility(View.VISIBLE);
			lvRight.setVisibility(View.VISIBLE);
		} else if ("5".equals(icon)) {
			lvTeam.setImageResource(icons[4]);
			pos = 4;
			// 初始状态将左边的箭头隐藏,右边的箭头显示
			lvLeft.setVisibility(View.VISIBLE);
			lvRight.setVisibility(View.VISIBLE);
		} else if ("6".equals(icon)) {
			lvTeam.setImageResource(icons[5]);
			pos = 5;
			// 初始状态将左边的箭头隐藏,右边的箭头显示
			lvLeft.setVisibility(View.VISIBLE);
			lvRight.setVisibility(View.VISIBLE);
		} else if ("7".equals(icon)) {
			lvTeam.setImageResource(icons[6]);
			pos = 6;
			// 初始状态将左边的箭头隐藏,右边的箭头显示
			lvLeft.setVisibility(View.VISIBLE);
			lvRight.setVisibility(View.VISIBLE);
		} else if ("8".equals(icon)) {
			lvTeam.setImageResource(icons[7]);
			pos = 7;
			// 初始状态将左边的箭头隐藏,右边的箭头显示
			lvLeft.setVisibility(View.VISIBLE);
			lvRight.setVisibility(View.VISIBLE);
		} else if ("9".equals(icon)) {
			lvTeam.setImageResource(icons[8]);
			pos = 8;
			// 初始状态将左边的箭头隐藏,右边的箭头显示
			lvLeft.setVisibility(View.VISIBLE);
			lvRight.setVisibility(View.VISIBLE);
		} else if ("10".equals(icon)) {
			lvTeam.setImageResource(icons[9]);
			pos = 9;
			// 初始状态将左边的箭头隐藏,右边的箭头显示
			lvLeft.setVisibility(View.VISIBLE);
			lvRight.setVisibility(View.INVISIBLE);
		}

		lvTeam.setImageResource(icons[pos]);
		lvDelete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (popupWindow != null && popupWindow.isShowing()) {
					popupWindow.dismiss();
					popupWindow = null;
					headView = null;
				}
			}
		});
		
		lvLeft.setOnClickListener(this);
		lvRight.setOnClickListener(this);
		tvSave.setOnClickListener(this);

		popupWindow = new PopupWindow(headView,
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT);
		popupWindow.setFocusable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		backgroundAlpha(this, 0.3f);
		popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
		popupWindow
				.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		popupWindow.setAnimationStyle(R.style.AnimationAlphaFade);
		popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
			@Override
			public void onDismiss() {
				backgroundAlpha(ClassInfoActivity.this, 1.0f);
				// PopUpWindow消失的将选择位置清空
				pos = 0;
			}
		});
		popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER,
				0, 0);
	}

	/**
	 * 改变屏幕透明度 方法
	 *
	 * @param bgAlpha
	 */
	public void backgroundAlpha(Activity activity, float bgAlpha) {
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.alpha = bgAlpha;
		activity.getWindow().setAttributes(lp);
	}

	/**
	 * 点击右箭头同步状态的方法
	 */
	private void syncRightState() {
		pos++;
		if (lvLeft.getVisibility() != View.VISIBLE) {
			lvLeft.setVisibility(View.VISIBLE);
		}
		if (pos == icons.length) {
			pos = 9;
			return;
		} else {
			lvRight.setVisibility(View.VISIBLE);
			if (pos == icons.length - 1) {
				lvRight.setVisibility(View.INVISIBLE);
			}
			lvTeam.setImageResource(icons[pos]);
		}
	}

	/**
	 * 点击左箭头同步状态的方法
	 */
	private void syncLeftState() {
		pos--;
		if (lvRight.getVisibility() != View.VISIBLE) {
			lvRight.setVisibility(View.VISIBLE);
		}

		if (pos < 0) {
			pos = 0;
			return;
		} else {
			lvLeft.setVisibility(View.VISIBLE);
			if (pos == 0) {
				lvLeft.setVisibility(View.INVISIBLE);
			}
			lvTeam.setImageResource(icons[pos]);
		}
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(myBroadcastReceiver);
		super.onDestroy();
	}

	/**
	 * 广播接受者
	 */
	public class MyBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// 收到广播受重新刷新页面数据
			if ("refreshbroadcast".equals(intent.getAction())) {
				Log.i("test", "接受到广播了");
				http(NORMAL);
			}
		}
	}
}
